const wx2my = require('./wx2my');
//app.js
import { Fm } from './utils/tools.js';

App({
  onLaunch: function (e) {
    this.fm = new Fm();
   
  }, 
  
  /*
   cb(flag, name, result, data)
   name 为自已定义的标识符，回调会原样返回
    flag = 1  选择文件成功，result选择的结果，状态显示为正在上传文件
   flag = -1 选择文件失败，result为失败结果
    flag = 2 为上传文件成功，result为文件id传给服务器, data为上传文件src等详细数据
   flag = -2 为上传文件失败，result为失败结果
   flag = -3 为文件保存失败，result为失败原因
    flag = -4 最多只能上传10张
   */
  uploadFile: function (name, cb, data, params, count) {
    console.log(11111);
    //name自定标识,cb回调函数,data其它表单json,params chooseImage参数{count:1}
    data.token = wx2my.getStorageSync('token') ? wx2my.getStorageSync('token') : '';
    var that = this;
    var count = count ? count : 1;
    var chooseParams = {
      count: count,
      success: function (res1) {
        console.log(res1);
        typeof cb == "function" && cb(1, name, res1);
        var tempFilePaths = res1.tempFilePaths;

        for (var tempFileIndex in tempFilePaths) {
          wx2my.uploadFile({
            url: that.globalData.host + '/user_api/upload',
            filePath: tempFilePaths[tempFileIndex],
            name: 'file',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            formData: data || {},
            success: function (res3) {
              console.log(res3);
              res3 = res3.data;
              res3 = res3.trim();
              res3 = JSON.parse(res3);
              console.log(res3);

              if (res3.code == 1) {
                typeof cb == "function" && cb(2, name, res3.data);
              } else {
                wx2my.showToast({
                  icon: 'none',
                  title: res3.msg
                });
              }
            },
            fail: function (res2) {
              typeof cb == "function" && cb(-2, name, res2);
            }
          });
        }
      },
      fail: function (res1) {
        typeof cb == "function" && cb(-1, name, res1);
      }
    };

    if (typeof params == 'object') {
      for (var i in params) chooseParams[i] = params[i];
    }

    wx2my.chooseImage(chooseParams);
  },
  //请求接口
  wxRequest: function (url, data, successCB, type) {
    var that = this;
    var requestUrl = that.globalData.host + '/user_api/' + url;
    var requestMethod = type ? type : 'POST';
    var requestConType = 'application/x-www-form-urlencoded';
    if (that.fm.gain('token') && !data.shop_token) data.token = that.fm.gain('token');

    if (typeof url != 'string') {
      requestUrl = url.url;
      requestMethod = url.method;
      requestConType = url.conType;
    }

    my.request({
      url: requestUrl,
      header: {
        'content-type': requestConType
      },
      data: data,
      method: requestMethod,
      success: function (res) {
        // console.log("app.js", res)
        //      typeof successCB == "function" && successCB(res);
        if (res.data.code == -5) {
          wx2my.removeStorageSync('token'); // my.reLaunch({
          //   url: '/pages/user_authorization/user_authorization?to=' + 5,
          // });

          console.log('未授权');
        } else if (res.data.code == -101) {
          wx2my.redirectTo({
            url: '/pages/agent_login/agent_login'
          });
        } else {
          typeof successCB == "function" && successCB(res);
        }
      },
      fail: function (err) {
        console.log(err);
      }
    });
  },
  location_href() {
    return getCurrentPages()[getCurrentPages().length - 1].route; //获取当前页面的对象
  },

  globalData: {
    // host: 'https://ygd.yunchuang.store/'
    host: 'https://ycdrone.wteam.club'
    
  },
  toast: (msg, time) => {
    wx2my.showToast({
      icon: 'none',
      mask: true,
      title: msg,
      duration: time ? time : 2000
    });
  },
  loading: msg => {
    wx2my.showLoading({
      mask: true,
      title: msg ? msg : '加载中..'
    });
    setTimeout(() => {
      wx2my.hideLoading();
    }, 5000);
  },

  lookOther(){
    wx2my.navigateTo({
      url: '/pages/near_store/near_store'
    });
  }
});